<?php

namespace app\common\controller;

class WorkerApi extends Base
{
    //绑定uid
    public static function bin_uid($conn,$uid){
        global $ws_worker;
        $conn->uid  = $uid;
        $ws_worker->uidConnections[$conn->uid] = $conn;
    }

    //绑定组
    public static function bin_group($conn,$uid,$group_id){
        global $ws_worker;
        $conn->group_id  = $group_id;
        $ws_worker->groupConnections[$group_id][$uid] = $uid;
    }
    //向绑定uid发送
    public static function send_uid($uid,$data){
        global $ws_worker;
        //需要遍历在本直播间用户给每个用户推送聊天内容
        if (isset($ws_worker->uidConnections[$uid])) {
            // 获取之前用户的链接
            $conn = $ws_worker->uidConnections[$uid];
            $conn->send($data);
        }
    }
    //向绑定组发
    public static function send_group($group_id,$data){
        global $ws_worker;
        //需要遍历在本直播间用户给每个用户推送聊天内容
        if (isset($ws_worker->uidConnections[$group_id])) {
            foreach ($ws_worker->uidConnections[$group_id] as  $key=>$conn) {
                $conn->send($data);
            }
        }
    }
    //所有人发信息
    public static function send_all($message)
    {
        if (empty($message)){
            echo "没有内容\n";
        }
        global $ws_worker;
        if (!empty($ws_worker->uidConnections)) {
            foreach ($ws_worker->uidConnections as  $key=>$conn) {
                $conn->send($message);
            }
        }else{
            echo "没有内容2\n";
        }
    }

    //踢出uid
    public static function del_uid($uid){
        global $ws_worker;
        unset($ws_worker->uidConnections[$uid]);
    }
}